package com.blackjack.cards
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.Point;

	public class Card extends Sprite
	{
		private var _open:Boolean = false;
		
		private var _suit:String;
		private var _rank:String;
		
		private var _frontTexture:MovieClip;
		private var _backTexture:MovieClip;
		
		public function Card(suit:String,rank:String)
		{
			_suit = suit;
			_rank = rank;
			initCard();
			_backTexture.visible = true;
			_frontTexture.visible = false;
			addChild(_backTexture);
			addChild(_frontTexture);
		}
		
		public function get open():Boolean{return _open;}
		public function set open(value:Boolean):void{_open = value;}
		
		public function get suit():String{return _suit;}
		public function get rank():String{return _rank;}
		public function get frontTexture():MovieClip{return _frontTexture;}
		public function get backTextuere():MovieClip{return _backTexture;}
		
		public function update():void{
			if(open){
				_backTexture.visible = false;
				_frontTexture.visible = true;
			}else{
				_backTexture.visible = true;
				_frontTexture.visible = false;
			}
		}
		
		private function initCard():void{
			_backTexture = new Texture_Card_Back();
			if(_suit == "Club"){
				createClubs();
			}else if(_suit == "Diamond"){
				createDiamonds();
			}else if(_suit == "Heart"){
				createHearts();
			}else if(_suit == "Spade"){
				createSpades();
			}else{
				throw(new Error("Suit does not exist"));
			}
		}
		
		private function createHearts():void{
			switch (_rank){
				case "2":
					_frontTexture = new Texture_Hearts_2();
					break;
				case "3":
					_frontTexture = new Texture_Hearts_3();
					break;
				case "4":
					_frontTexture = new Texture_Hearts_4();
					break;
				case "5":
					_frontTexture = new Texture_Hearts_5();
					break;
				case "6":
					_frontTexture = new Texture_Hearts_6();
					break;
				case "7":
					_frontTexture = new Texture_Hearts_7();
					break;
				case "8":
					_frontTexture = new Texture_Hearts_8();
					break;
				case "9":
					_frontTexture = new Texture_Hearts_9();
					break;
				case "10":
					_frontTexture = new Texture_Hearts_10();
					break;
				case "Jack":
					_frontTexture = new Texture_Hearts_Jack();
					break;
				case "Queen":
					_frontTexture = new Texture_Hearts_Queen();
					break;
				case "King":
					_frontTexture = new Texture_Hearts_King();
					break;
				case "Ace":
					_frontTexture = new Texture_Hearts_Ace();
					break;
				default:
					throw(new Error("Rank of Card not found"));
					break;
			}
		}
		
		private function createDiamonds():void{
			switch (_rank){
				case "2":
					_frontTexture = new Texture_Diamonds_2();
					break;
				case "3":
					_frontTexture = new Texture_Diamonds_3();
					break;
				case "4":
					_frontTexture = new Texture_Diamonds_4();
					break;
				case "5":
					_frontTexture = new Texture_Diamonds_5();
					break;
				case "6":
					_frontTexture = new Texture_Diamonds_6();
					break;
				case "7":
					_frontTexture = new Texture_Diamonds_7();
					break;
				case "8":
					_frontTexture = new Texture_Diamonds_8();
					break;
				case "9":
					_frontTexture = new Texture_Diamonds_9();
					break;
				case "10":
					_frontTexture = new Texture_Diamonds_10();
					break;
				case "Jack":
					_frontTexture = new Texture_Diamonds_Jack();
					break;
				case "Queen":
					_frontTexture = new Texture_Diamonds_Queen();
					break;
				case "King":
					_frontTexture = new Texture_Diamonds_King();
					break;
				case "Ace":
					_frontTexture = new Texture_Diamonds_Ace();
					break;
				default:
					throw(new Error("Rank of Card not found"));
					break;
			}
		}
		
		private function createSpades():void{
			switch (_rank){
				case "2":
					_frontTexture = new Texture_Spades_2();
					break;
				case "3":
					_frontTexture = new Texture_Spades_3();
					break;
				case "4":
					_frontTexture = new Texture_Spades_4();
					break;
				case "5":
					_frontTexture = new Texture_Spades_5();
					break;
				case "6":
					_frontTexture = new Texture_Spades_6();
					break;
				case "7":
					_frontTexture = new Texture_Spades_7();
					break;
				case "8":
					_frontTexture = new Texture_Spades_8();
					break;
				case "9":
					_frontTexture = new Texture_Spades_9();
					break;
				case "10":
					_frontTexture = new Texture_Spades_10();
					break;
				case "Jack":
					_frontTexture = new Texture_Spades_Jack();
					break;
				case "Queen":
					_frontTexture = new Texture_Spades_Queen();
					break;
				case "King":
					_frontTexture = new Texture_Spades_King();
					break;
				case "Ace":
					_frontTexture = new Texture_Spades_Ace();
					break;
				default:
					throw(new Error("Rank of Card not found"));
					break;
			}
		}
		
		private function createClubs():void{
			switch (_rank){
				case "2":
					_frontTexture = new Texture_Clubs_2();
					break;
				case "3":
					_frontTexture = new Texture_Clubs_3();
					break;
				case "4":
					_frontTexture = new Texture_Clubs_4();
					break;
				case "5":
					_frontTexture = new Texture_Clubs_5();
					break;
				case "6":
					_frontTexture = new Texture_Clubs_6();
					break;
				case "7":
					_frontTexture = new Texture_Clubs_7();
					break;
				case "8":
					_frontTexture = new Texture_Clubs_8();
					break;
				case "9":
					_frontTexture = new Texture_Clubs_9();
					break;
				case "10":
					_frontTexture = new Texture_Clubs_10();
					break;
				case "Jack":
					_frontTexture = new Texture_Clubs_Jack();
					break;
				case "Queen":
					_frontTexture = new Texture_Clubs_Queen();
					break;
				case "King":
					_frontTexture = new Texture_Clubs_King();
					break;
				case "Ace":
					_frontTexture = new Texture_Clubs_Ace();
					break;
				default:
					throw(new Error("Rank of Card not found"));
					break;
			}
		}

	}
}